#include "Model.h"

CModel::CModel(void)
{

}

CModel::~CModel(void)
{
	ReleaseCOM(mFX);

	ReleaseCOM(mMesh);
	for(int i = 0; i < mTex.size(); ++i)
		ReleaseCOM(mTex[i]);

	ReleaseCOM(mWhiteTex);
}

void CModel::OnLostDevice()
{
	mFX->OnLostDevice();
}

void CModel::OnResetDevice()
{
	mFX->OnResetDevice();
}


void CModel::LoadModelFromXFile(const char *strXFileName)
{
	mLight.dirW    = D3DXVECTOR3(0.0f, 0.0f, -1.0f);
	D3DXVec3Normalize(&mLight.dirW, &mLight.dirW);
	mLight.ambient = D3DXCOLOR(0.5f, 0.5f, 0.5f, 0.2f);
	mLight.diffuse = D3DXCOLOR(0.8f, 0.5f, 0.2f, 1.0f);
	mLight.spec    = D3DXCOLOR(0.8f, 0.8f, 0.2f, 1.0f);

	//SetCurrentDirectory(L"Resources");
	LoadXFile(strXFileName, &mMesh, mMtrl, mTex);
	D3DXMatrixIdentity(&mWorld);

	D3DXCreateTextureFromFile(gd3dDevice, L"whitetex.dds", &mWhiteTex);

	BuildFX();
}

void CModel::BuildFX()
{
	// Create the FX from a .fx file.
	ID3DXBuffer* errors = 0;
	D3DXCreateEffectFromFile(gd3dDevice, L"PhongDirLtTex.fx", 
		0, 0, D3DXSHADER_DEBUG, 0, &mFX, &errors);
	if( errors ) 
	{
		MessageBox(0, (LPCWSTR)errors->GetBufferPointer(), 0, 0);
	}

	// Obtain handles.
	mhTech            = mFX->GetTechniqueByName("PhongDirLtTexTech");
	mhWVP             = mFX->GetParameterByName(0, "gWVP");
	mhWorldInvTrans   = mFX->GetParameterByName(0, "gWorldInvTrans");
	mhMtrl            = mFX->GetParameterByName(0, "gMtrl");
	mhLight           = mFX->GetParameterByName(0, "gLight");
	mhEyePos          = mFX->GetParameterByName(0, "gEyePosW");
	mhWorld           = mFX->GetParameterByName(0, "gWorld");
	mhTex             = mFX->GetParameterByName(0, "gTex");
}


void CModel::Update(float dt)
{
	mFX->SetValue(mhEyePos, &CCamera::GetInstance()->GetEyePos(), sizeof(D3DXVECTOR3));
}

void CModel::Render()
{
	D3DXMATRIX mvpMat =  mWorld * CCamera::GetInstance()->GetViewMatrix() * CCamera::GetInstance()->GetProjMatrix();
	D3DXMATRIX max2,max3  ;

	/*D3DXMatrixScaling(&max2 , 0.1f,0.1f,0.1f);
	mvpMat*=max2;*/

	//D3DXMatrixTranslation(&max3,0,10,0.0f);
	//mvpMat*=max3;

	mFX->SetValue(mhLight, &mLight, sizeof(DirLight));
	mFX->SetMatrix(mhWVP, &mvpMat);
	D3DXMATRIX worldInvTrans;
	D3DXMatrixInverse(&worldInvTrans, 0, &mWorld);
	D3DXMatrixTranspose(&worldInvTrans, &worldInvTrans);
	mFX->SetMatrix(mhWorldInvTrans, &worldInvTrans);
	mFX->SetMatrix(mhWorld, &mWorld);


	mFX->SetTechnique(mhTech);
	UINT numPasses = 0;
	mFX->Begin(&numPasses, 0);
	mFX->BeginPass(0);

	for(int j = 0; j < mMtrl.size(); ++j)
	{
		mFX->SetValue(mhMtrl, &mMtrl[j], sizeof(Mtrl));
	
		// If there is a texture, then use.
		if(mTex[j] != 0)
		{
			mFX->SetTexture(mhTex, mTex[j]);
		}

		// But if not, then set a pure white texture.  When the texture color
		// is multiplied by the color from lighting, it is like multiplying by
		// 1 and won't change the color from lighting.
		else
		{
		mFX->SetTexture(mhTex, mWhiteTex);
		}
	
		mFX->CommitChanges();
		mMesh->DrawSubset(j);
	}
	mFX->EndPass();
	mFX->End();
}